<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
      <el-form-item label="码表" prop="tableName" style="width: 200px">
        <el-select v-model="queryParams.tableName" placeholder="请选择" >
              <el-option v-for="item in codeTables" :key="item.tableName" :label="item.txt" :value="item.tableName" />
            </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleQuery">查询</el-button>
        <el-button  @click="addData">新建</el-button>
      </el-form-item>
    </el-form>

    <test :parameterData="parameterData" :parameterVal="parameterVal"></test>

    <el-table v-loading="loading" :data="tableData">
      <el-table-column label="码值" prop="code"></el-table-column>
      <el-table-column label="码值含义" prop="txt"></el-table-column>
      <el-table-column label="操作">
        <template #default="scope">
        <el-tooltip content="修改" placement="top">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row, 'edit')" ></el-button>
        </el-tooltip>
        <el-tooltip content="删除" placement="top">
          <el-button link type="danger" icon="Delete" @click="handleDelete(scope.row)"></el-button>
        </el-tooltip>
      </template>
      </el-table-column>
    </el-table>

    <el-dialog v-model="dialogFormVisible" :title="handleType ==='edit'?'修改码值': '新增码值'" width="500">
      <el-form :model="dialogForm">
        <el-form-item label="表名" :label-width="formLabelWidth">
          <el-input v-model="dialogForm.tableName" autocomplete="off" />
        </el-form-item>
        <el-form-item label="码值" :label-width="formLabelWidth">
          <el-input v-model="dialogForm.code" autocomplete="off" />
        </el-form-item>
        <el-form-item label="码值说明" :label-width="formLabelWidth">
          <el-input v-model="dialogForm.txt" autocomplete="off" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取消</el-button>
          <el-button type="primary" @click="handleSave">
            保存
          </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="TableConfgig">
import { codeQuery, codeSave, codeDelete,tableQuery} from '@/api/safeguard/index.js';
const queryParams = ref({
  tableName: ''
})
const showSearch = ref(true);
const loading = ref(false);
const tableData = ref([]);
const codeTables = ref([])
const handleType= ref()

const dialogFormVisible = ref(false)
const dialogForm = ref({
  tableName: '',
  code: '',
  txt: '',
})
const formLabelWidth = '140px'

function getList () {
  const data = queryParams.value.tableName
  if(data){
    loading.value = true;
    tableData.value = []
    codeQuery(data).then(res => {
      if(data) {
        tableData.value = res.data || []
      } else {
        tableData.value = res.data.codeVlaues || []
      }
      tableData.value.length > 0 && tableData.value.map(item => {
        item['tableName'] = data || res.tableName
      })
    }).finally(() => {
      loading.value = false;
    })
  }
}

function initQueryParams(){
  tableQuery('').then(res => {
    console.info(res.data)
    codeTables.value = res.data
  })
}

function handleQuery() {
  initQueryParams()
  getList();
}

 /** 添加 */
 function addData () {
  dialogFormVisible.value = true
}
 /** 编辑 */
function handleUpdate (row,type) {
  handleType.value = type
  dialogForm.value = row
  dialogFormVisible.value = true
}

function handleSave() {  
  // 保存成功刷新数据
  codeSave({...dialogForm.value}).then(() => {
    dialogFormVisible.value = false
    getList();
  })
}

function handleDelete(row){
  const tableName = queryParams.value.tableName
  const code = row.code || 0
  codeDelete(tableName,code).then(() => {
    dialogFormVisible.value = false
    getList();
  })
}

handleQuery()
</script>